#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    wasd = Qt::Key_Down;
    wasd1 = Qt::Key_Down;

    timer = new QTimer;
    connect(timer, SIGNAL(timeout()), this, SLOT(slots_move1()));
    connect(timer, SIGNAL(timeout()), this, SLOT(slots_move()));
    timer->start(80);

    creat_food();
    snake.push_back(food);
    food->setStyleSheet("background-color: rgb(255, 0, 0);");

    creat_food();
    snake1.push_back(food);
    food->setStyleSheet("background-color: rgb(0, 255, 0);");

    creat_food();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::keyPressEvent(QKeyEvent* e)
{
    switch(e->key())
    {
    case Qt::Key_Up: if(wasd == Qt::Key_Down) break; wasd = e->key(); break;
    case Qt::Key_Down: if(wasd == Qt::Key_Up) break; wasd = e->key(); break;
    case Qt::Key_Left: if(wasd == Qt::Key_Right) break; wasd = e->key(); break;
    case Qt::Key_Right: if(wasd == Qt::Key_Left) break; wasd = e->key(); break;

    case Qt::Key_W: if(wasd1 == Qt::Key_S) break; wasd1 = e->key(); break;
    case Qt::Key_S: if(wasd1 == Qt::Key_W) break; wasd1 = e->key(); break;
    case Qt::Key_A: if(wasd1 == Qt::Key_D) break; wasd1 = e->key(); break;
    case Qt::Key_D: if(wasd1 == Qt::Key_A) break; wasd1 = e->key(); break;
    }
}

void Widget::slots_move(void)
{
    for(int i=snake.size()-1; i>0; i--)
    {
        snake[i]->move(snake[i-1]->x(), snake[i-1]->y());
    }

    int x = snake[0]->x();
    int y = snake[0]->y();

    switch(wasd)
    {
    case Qt::Key_Up:y -= 10; break;
    case Qt::Key_Down: y += 10; break;
    case Qt::Key_Left: x -= 10; break;
    case Qt::Key_Right: x += 10; break;
    }
    snake[0]->move(x, y);

    for(int i=1; i<snake.size(); i++)
    {
        if(x == snake[i]->x() && y == snake[i]->y())
        {
            exit(0);
        }
    }

    if(x < 0 || x > 600 || y < 0 || y > 400)
    {
        exit(0);
    }

    if(snake[0]->x() == food->x() && snake[0]->y() == food->y())
    {
        snake.push_back(food);
        food->setStyleSheet("background-color: rgb(255, 0, 0);");
        creat_food();
    }
}

void Widget::slots_move1(void)
{
    for(int i=snake1.size()-1; i>0; i--)
    {
        snake1[i]->move(snake1[i-1]->x(), snake1[i-1]->y());
    }

    int x = snake1[0]->x();
    int y = snake1[0]->y();

    switch(wasd1)
    {
    case Qt::Key_W: y -= 10; break;
    case Qt::Key_S: y += 10; break;
    case Qt::Key_A: x -= 10; break;
    case Qt::Key_D: x += 10; break;
    }
    snake1[0]->move(x, y);

    for(int i=1; i<snake1.size(); i++)
    {
        if(x == snake1[i]->x() && y == snake1[i]->y())
        {
            exit(0);
        }
    }

    if(x < 0 || x > 600 || y < 0 || y > 400)
    {
        exit(0);
    }

    if(snake1[0]->x() == food->x() && snake1[0]->y() == food->y())
    {
        snake1.push_back(food);
        food->setStyleSheet("background-color: rgb(0, 255, 0);");
        creat_food();
    }
}

void Widget::creat_food(void)
{
    food = new Food(this);
    int x = rand() % (this->width()/food->width()) * food->width();
    int y = rand() % (this->height()/food->height()) * food->height();
    food->move(x, y);
    food->show();
}
